package com.kamloo.stg.activity;

import com.kamloo.stg.data.db.MySQLiteHelper;
import com.kamloo.stg.R;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class SplashScreen extends Activity {

	protected class DbUpdater extends AsyncTask<Void, Void, Void> 
	{
		
	    @Override 
	    protected void onPreExecute() 
	    { 
	        super.onPreExecute();
	        blink();
	    }
	     
	    private void blink(){
	    	final Handler handler = new Handler();
	    	new Thread(new Runnable() {	    	
	    		public void run() {
	    			int timeToBlink = 500;
	    			try {
	    				Thread.sleep(timeToBlink);
	    			} catch (Exception e) {}
	    	    
	    			handler.post(new Runnable() {
	    				public void run() {
	    					TextView txt = (TextView) findViewById(R.id.splashScreen_LoadingText);
	    	                if(txt.getVisibility() == View.VISIBLE) {
	    	                        txt.setVisibility(View.INVISIBLE);
	    	                } else {
	    	                	txt.setVisibility(View.VISIBLE);
	    	                }
	    	                blink();
	    	            }});
	    	     }
    		}).start();
	    }
	    

		protected Void doInBackground(Void... voids) {
			MySQLiteHelper dbHelper = new MySQLiteHelper(getApplicationContext());                
            dbHelper.getReadableDatabase();            
            dbHelper.close();
			return null;
		}
		

	     // -- called as soon as doInBackground method completes		
	    protected void onPostExecute(Void params) {	        
			Intent intent = new Intent(SplashScreen.this, MainScreen.class);
     		startActivity(intent);
     		
     		finish();
	    }
 
	} 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        
        new DbUpdater().execute();
    }    
}